package com.mumu.xiaodu

import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.mumu.xiaodu.databinding.ActivityBookmarkManagerBinding

class BookmarkManagerActivity : FragmentActivity() {
    
    private lateinit var binding: ActivityBookmarkManagerBinding
    private lateinit var bookmarkAdapter: BookmarkAdapter
    private lateinit var bookmarkManager: BookmarkManager
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBookmarkManagerBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        bookmarkManager = BookmarkManager(this)
        initViews()
        loadBookmarks()
    }
    
    private fun initViews() {
        bookmarkAdapter = BookmarkAdapter(
            onBookmarkClick = { bookmark ->
                openWebsite(bookmark.url)
            },
            onAddBookmarkClick = {
                showAddBookmarkDialog()
            }
        )
        
        binding.rvBookmarks.layoutManager = GridLayoutManager(this, 4)
        binding.rvBookmarks.adapter = bookmarkAdapter
        
        // 添加网格间距装饰器 - 与主界面保持一致
        val spacingInPixels = resources.getDimensionPixelSize(R.dimen.grid_spacing)
        binding.rvBookmarks.addItemDecoration(
            GridSpacingItemDecoration(4, spacingInPixels, true)
        )
        
        binding.btnBack.setOnClickListener {
            finish()
        }
    }
    
    private fun loadBookmarks() {
        val bookmarks = bookmarkManager.getAllBookmarks()
        bookmarkAdapter.updateBookmarks(bookmarks)
        
        if (bookmarks.isEmpty()) {
            binding.tvEmptyMessage.visibility = android.view.View.VISIBLE
            binding.rvBookmarks.visibility = android.view.View.GONE
        } else {
            binding.tvEmptyMessage.visibility = android.view.View.GONE
            binding.rvBookmarks.visibility = android.view.View.VISIBLE
        }
    }
    
    private fun openWebsite(url: String) {
        val intent = Intent(this, WebViewActivity::class.java)
        intent.putExtra("url", url)
        startActivity(intent)
    }
    
    private fun showAddBookmarkDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("添加书签")
        
        // 创建输入布局
        val layout = layoutInflater.inflate(android.R.layout.simple_list_item_2, null)
        val titleInput = EditText(this).apply {
            hint = "网站标题"
            setText("")
        }
        val urlInput = EditText(this).apply {
            hint = "网站地址 (如: https://www.example.com)"
            setText("")
        }
        
        val container = android.widget.LinearLayout(this).apply {
            orientation = android.widget.LinearLayout.VERTICAL
            setPadding(50, 40, 50, 40)
            addView(titleInput)
            addView(urlInput)
        }
        
        builder.setView(container)
        
        builder.setPositiveButton("添加") { _, _ ->
            val title = titleInput.text.toString().trim()
            val url = urlInput.text.toString().trim()
            
            if (title.isNotEmpty() && url.isNotEmpty()) {
                val bookmark = Bookmark(title, url, "custom")
                bookmarkManager.addBookmark(bookmark)
                loadBookmarks()
                Toast.makeText(this, "书签添加成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show()
            }
        }
        
        builder.setNegativeButton("取消") { dialog, _ ->
            dialog.dismiss()
        }
        
        builder.show()
    }
} 